package com.tange365.demo;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.util.Log;

import androidx.multidex.MultiDexApplication;

import com.alibaba.fastjson.JSONObject;
import com.tg.app.BuildConfig;
import com.tg.app.TGSdk;
import com.tg.appcommon.android.TGLog;
import com.tg.appcommon.sdk.TGMessageType;


public class DemoApplication extends MultiDexApplication {

    private static final String TAG = "DemoApplication";

    @Override
    public void onCreate() {
        super.onCreate();
        TGSdk.getInstance().initApp(this, mTGNotifyListener, BuildConfig.DEBUG);
        TGLog.i(TAG, "Program start ..............");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        TGSdk.getInstance().onConfigurationChanged(this);
    }

    private final TGSdk.TGNotifyListener mTGNotifyListener = (messageId, param) -> {
        Intent intent = null;
        switch (messageId){
            case TGMessageType.MSG_ACCOUNT_LOGOUT:
                Log.d("TanGeApplication", "你的账号被挤下线了，请处理");
                break;
            case TGMessageType.MSG_ADD_DEVICE_SUCCUSS:
                Log.d("TanGeApplication", "增加设备成功，设备名称为：" + param);
                JSONObject json_test = JSONObject.parseObject(param);
                String name = json_test.getString(TGSdk.KEY_NAME);
                Log.d("TanGeApplication", "增加设备成功，设备名称为 name：" + name);
                break;
            case TGMessageType.MSG_DEL_DEVICE_SUCCUSS:
                Log.d("TanGeApplication", "删除设备成功，设备UUID为：" + param);
                JSONObject jsonObject = JSONObject.parseObject(param);
                String uuid = jsonObject.getString(TGSdk.KEY_UUID);
                Log.d("TanGeApplication", "删除设备成功，设备UUID为：" + uuid);
                break;
            default:
                break;
        }
    };

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        TGSdk.getInstance().attachBaseContext(base);
    }

    @Override
    public void onTrimMemory(int level) {
        Log.i("TanGeApplicationTAG", "onTrimMemory: level = " + level);
        try {
            super.onTrimMemory(level);
        } catch (Throwable ex) {
            Log.i("TanGeApplicationTAG", "onTrimMemory: err = " + ex);
        }
    }
}
